/**
 * Bookshelf控制器模块
 * controller\bookshelf.js
 */

const express = require("express");

const util = require("../common/util");
/**
 * @typedef {BookshelfDB}
 */
const BookshelfDB = require("../model/sqlite/bookshelf");

/**
 * 获取书架信息
 * @param {express.Request} req  请求
 * @param {express.Response} res  响应
 */
function find(req, res) {
  (async function () {
    let db = BookshelfDB.getInstance();
    await db.connect();
    let result = await db.find(req.params.id, req.session.user.id); //await db.close();
    util.logFormat(`获取【${req.params.id}】书架信息%O`, result);
    res.json(
      util.FormatJSONData(200, `获取书架信息【${req.params.id}】`, result)
    );
  })();
}

/**
 * 获取书籍信息列表
 * @param {express.Request} req  请求
 * @param {express.Response} res  响应
 */
function findAll(req, res) {
  (async function () {
    //1.处理输入的数据
    //FIXME: 输入数据的正确性校验，一致性校验，完整性校验
    let userId = req.session.user.id;
    let orderBy = req.query.order_by ? req.query.order_by : "id";
    let sort = req.query.sort ? eq.query.sort : "desc";
    let limit = req.query.limit ? req.query.limit : -1;
    let offset = req.query.offset ? req.query.offset : -1; //2. 访问数据
    let db = BookshelfDB.getInstance();
    await db.connect();
    let result = await db.findAll(userId, orderBy, sort, limit, offset); //await db.close(); //3.输出数据
    util.logFormat(`获取指定用户【${userId}】的书架书籍信息%O`, result);
    res.json(util.FormatJSONData(200, `获取用户书架书籍信息列表`, result));
  })();
}

/**
 * 新增书籍信息
 * @param {express.Request} req  请求
 * @param {express.Response} res  响应
 */
function add(req, res) {
  (async function () {
    //FIXME:req.body的数据安全性校验
    let bookshelf = {
      userId: req.session.user.id,
      bookId: req.body.bookId,
      createdTime: Date.now(),
      updatedTime: Date.now(),
    };
    let db = BookshelfDB.getInstance();
    await db.connect();
    let result = await db.add(bookshelf); //await db.close();
    util.log(`新增书架书籍信息：lastID->${result}`);
    res.json(
      util.FormatJSONData(201, `书架中添加书籍成功`, { lastID: result })
    );
  })();
}

/**
 * 更新书籍信息
 * @param {express.Request} req  请求
 * @param {express.Response} res  响应
 */
function update(req, res) {
  (async function () {
    //FIXME:req.body的数据安全性校验
    let bookshelf = {
      id: req.params.id,
      readStatus: req.body.readStatus,
      ranking: req.body.ranking,
      updatedTime: Date.now(),
    };

    let db = BookshelfDB.getInstance();
    await db.connect();
    let result = await db.update(bookshelf); //await db.close();
    util.log(`更新书架中的书籍相关信息：changes->${result}`);
    res.json(
      util.FormatJSONData(200, `更新书架中的书籍相关信息`, { changes: result })
    );
  })();
}

/**
 * 删除书籍信息
 * @param {express.Request} req  请求
 * @param {express.Response} res  响应
 */
function remove(req, res) {
  (async function () {
    //FIXME:数据合法性校验
    let bookshelfId = req.params.id;
    let db = BookshelfDB.getInstance();
    await db.connect();
    let result = await db.remove(bookshelfId); //await db.close();
    util.log(`删除书架中的书籍信息：changes->${result}`);
    res.json(util.FormatJSONData(204, `删除书架中的书籍`, { changes: result }));
  })();
}

/**
 * 获取书籍总数
 * @param {express.Request} req  请求
 * @param {express.Response} res  响应
 */
function getCount(req, res) {
  (async function () {
    //FIXME:数据合法性校验
    let userId = req.session.user.id;
    let db = BookshelfDB.getInstance();
    await db.connect();
    let result = await db.getCount(userId); //await db.close();
    util.logFormat("书架中书籍总数：%O", result);
    res.json(util.FormatJSONData(200, `获取书架中书籍总数`, result));
  })();
}

/**
 * 获取书籍总数
 * @param {express.Request} req  请求
 * @param {express.Response} res  响应
 */
function updateReadStatus(req, res) {
  (async function () {
    //FIXME:数据合法性校验
    let id = req.params.id;
    let readStatus = req.body.readStatus;

    let db = BookshelfDB.getInstance();
    await db.connect();
    let result = await db.updateReadStatus(id, readStatus); //await db.close();
    util.log(`更新书架中书籍阅读状态：${readStatus}`);
    res.json(
      util.FormatJSONData(200, `更新书架中书籍阅读状态`, { changes: result })
    );
  })();
}

	/**
 * 更新书架中的书籍评分
 * @param {express.Request} req  请求
 * @param {express.Response} res  响应
 */
    function updateRanking(req, res) {
        (async function () {
          //FIXME:数据合法性校验
          let id = req.params.id;
          let ranking = req.body.ranking;
       
          let db = BookshelfDB.getInstance();
          await db.connect();
          let result = await db.updateRanking(id, ranking);
          //await db.close();
          util.log(`更新书架中书籍评分：${ranking}`);
          res.json(
            util.FormatJSONData(200, `更新书架中书籍阅读状态`, { changes: result })
          );
        })();
      }

module.exports = {
  find,
  findAll,
  add,
  update,
  remove,
  getCount,
  updateReadStatus,
  updateRanking,
};
